import PDF from '../assets/PDF.png';
import DOC from '../assets/DOC.svg';
import XLS from '../assets/XLS.svg';
import ZIP from '../assets/ZIP.svg';
import COMMON from '../assets/COMMON.svg';
export const useFile = (props, emits) => {
    /**
     * @description:  判断文件类型
     * @param: url
     * @returns: 'pdf' || 'image' || 'video' || 'excel' || 'word' || 'zip'
     */
    const judgeFileType = () => {
        if (/\.(mov|MOV|MP4|mp4|avi)$/g.test(props.file.fileId)) {
            return 'video';
        } else if (props.file.fileId && /\.(pdf|PDF)$/g.test(props.file.fileId)) {
            return PDF;
        } else if (props.file.fileId && /\.(jpg|image|jpeg|png|JPG|PNG|IMAGE|JPEG)$/g.test(props.file.fileId)) {
            return 'image';
        } else if (props.file.fileId && /\.(excel|xlsx|numbers|xls)$/g.test(props.file.fileId)) {
            return XLS;
        } else if (props.file.fileId && /\.(doc|docs|docx)$/g.test(props.file.fileId)) {
            return DOC;
        } else if (props.file.fileId && /\.(zip|rar)$/g.test(props.file.fileId)) {
            return ZIP;
        } else {
            return COMMON
        }
    };
    /**
     * @description: 下载
     */
    const download = () => {
        window.open(props.file.downloadUrl);
    };
    /**
     * @description: 删除附件  temporary 临时数据直接删除 不走接口
     */
    const deleteFile = () => {
        if (props.file.temporary) {
            emits('deleteFile', props.file);
            return;
        }
        const deleteFile = props.beforeDelete(props.file);
        if (deleteFile && deleteFile.then) {
            deleteFile
                .then(() => {
                    emits('deleteFile', props.file);
                })
                .catch(err => {
                    console.log(err, 'err');
                });
        }
    };
    /**
     * @description: 判断上传类型
     */
    const acceptFileType = fileFormat => {
        const arr = [];
        if (fileFormat.indexOf('pdf') != -1) {
            arr.push('application/pdf');
        }
        if (fileFormat.indexOf('png') != -1) {
            arr.push('image/png');
        }
        if (fileFormat.indexOf('jpg') != -1) {
            arr.push('image/jpg');
        }
        if (fileFormat.indexOf('jpeg') != -1) {
            arr.push('image/jpeg');
        }
        if (fileFormat.indexOf('gif') != -1) {
            arr.push('image/gif');
        }
        if (fileFormat.indexOf('video') != -1) {
            arr.push('*.video');
        }
        if (fileFormat.indexOf('excel') != -1) {
            arr.push('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel')
        }
        return arr.join(',');
    };
    /**
     * @description: 查看图片
     */
    const openProview = () => {
        emits('openProview', props.file);
    };

    return {
        judgeFileType,
        download,
        deleteFile,
        acceptFileType,
        openProview,
    };
};